﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YJKRGeometry;

namespace LightSmokeTemperatureCameraAdjust
{
	internal class MoveElementOperator
	{
		public void MoveElement(Document doc, List<Element> inputlstElements, CeilingOrFloorSearcher useSearcher, ElementAdjustForm form)
		{
			if (inputlstElements.Count == 0)
			{
				return;
			}
			List<KeyValuePair<Element, double>> list = new AdhesionCalculation().FindNearCeilingOrFloor(inputlstElements, useSearcher, form.FormDataSaver.m_bIsCeilOrFloor);
			if (list.Count == 0)
			{
				YJKMessageBox.Show("所选设备均已吸附于板底，不做调整");
				return;
			}
			int num = 0;
			int num2 = 0;
			ProgressBarForm progressBarForm = null;
			foreach (KeyValuePair<Element, double> keyValuePair in list)
			{
				if (!Geometry.IsEqual(keyValuePair.Value, 0.0))
				{
					num++;
				}
			}
			if (num > this.m_nUseFormNumber)
			{
				progressBarForm = new ProgressBarForm(num, ProgressBarStyle.Blocks, true);
			}
			if (progressBarForm != null)
			{
				progressBarForm.Show(CmdElementAdjust.m_hWndRevit);
			}
			Transaction transaction = new Transaction(doc, "MoveElement");
			try
			{
				transaction.Start();
				foreach (KeyValuePair<Element, double> keyValuePair2 in list)
				{
					if (!Geometry.IsEqual(keyValuePair2.Value, 0.0))
					{
						ElementTransformUtils.MoveElement(doc, keyValuePair2.Key.Id, XYZ.BasisZ * keyValuePair2.Value);
						num2++;
						if (progressBarForm != null)
						{
							if (progressBarForm.IsDisposed && progressBarForm.m_stopTag)
							{
								if (transaction.HasStarted())
								{
									transaction.RollBack();
								}
								return;
							}
							progressBarForm.RefreshProgressBar();
							if (progressBarForm.IsDisposed && progressBarForm.m_stopTag)
							{
								if (transaction.HasStarted())
								{
									transaction.RollBack();
								}
								return;
							}
						}
					}
				}
				transaction.Commit();
				if (progressBarForm != null)
				{
					progressBarForm.Dispose();
				}
				if (num2 == 0)
				{
					YJKMessageBox.Show("所选设备均已吸附于板底，不做调整");
				}
				else if (form.FormDataSaver.m_lstStrCategory.Count > 1)
				{
					YJKMessageBox.Show("共完成" + num2 + "个吸顶设备的调整");
				}
				else if (form.FormDataSaver.m_lstStrCategory.Count == 1)
				{
					int builtInCategory = (int)form.FormDataSaver.m_lstStrCategory[0];
					if (builtInCategory != -2008085)
					{
						if (builtInCategory != -2008079)
						{
							if (builtInCategory == -2001120)
							{
								YJKMessageBox.Show("共完成" + num2 + "盏吸顶灯的调整");
							}
						}
						else
						{
							YJKMessageBox.Show("共完成" + num2 + "个半球摄像机的调整");
						}
					}
					else
					{
						YJKMessageBox.Show("共完成" + num2 + "只火灾探测器的调整");
					}
				}
			}
			catch
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
		}

		private int m_nUseFormNumber;
	}
}
